package com.jerei.plugin.service.impl;

import com.jerei.plugin.api.IAdminuserService;
import com.jerei.plugin.api.IRoleService;
import com.jerei.plugin.dao.IWcmCommonAdminuserDao;
import com.jerei.plugin.entity.WcmCommonAdminuser;
import com.jerei.plugin.entity.WcmCommonRole;
import com.jerei.plugin.exceptions.PluginNotInstallException;
import com.jerei.plugin.service.IWcmCommonAdminuserService;
import com.jerei.plugin.utils.Plugins;
import com.jerei.utils.condition.ConditionEntity;
import com.jerei.utils.condition.ConditionOp;
import com.jerei.utils.condition.WhereEntity;
import com.jerei.utils.spring.PageBean;
import com.jerei.web.utils.UtilsPluginManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.stereotype.Service;
import ro.fortsoft.pf4j.Extension;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by sunfeihu on 2016/10/21.
 */
@Extension
@Service
public class WcmCommonAdminuserServiceImpl implements IWcmCommonAdminuserService, IAdminuserService {

    @Autowired
    private IWcmCommonAdminuserDao iWcmCommonAdminuserDao;

    @Override
    public WcmCommonAdminuser save(WcmCommonAdminuser entity) {
        return iWcmCommonAdminuserDao.save(entity);
    }

    @Override
    public int delete(Long id) {
        return iWcmCommonAdminuserDao.delete(id);
    }

    @Override
    public int update(WcmCommonAdminuser entity) {
        return iWcmCommonAdminuserDao.update(entity);
    }

    @Override
    public WcmCommonAdminuser find(Long id) {
        WcmCommonAdminuser adminuser = iWcmCommonAdminuserDao.find(id);
        try {
            IRoleService iRoleService = UtilsPluginManager.getExtension(IRoleService.class, Plugins.COMMON_ROLE);
            List<WcmCommonRole> roles = iRoleService.findRolesByAdminuser(adminuser.getId());
            if (null != roles && roles.size() > 0) {
                String roleStr = "";
                for (WcmCommonRole r : roles) {
                    roleStr += r.getId() + ",";
                }
                roleStr = roleStr.substring(0, roleStr.length() - 1);
                adminuser.setRoles(roleStr);
            }
        } catch (PluginNotInstallException e) {
            e.printStackTrace();
        }
        return adminuser;
    }


    @Override
    public void save(List<WcmCommonAdminuser> list) {
        iWcmCommonAdminuserDao.save(list);
    }

    @Override
    public void delete(List<Long> ids) {
        iWcmCommonAdminuserDao.delete(ids);
    }

    @Override
    public void update(List<WcmCommonAdminuser> list) {
        iWcmCommonAdminuserDao.update(list);
    }

    @Override
    public PageBean<WcmCommonAdminuser> page(int nowPage, int pageSize, ConditionEntity conditionEntity) {
        return iWcmCommonAdminuserDao.page(nowPage, pageSize, conditionEntity);
    }

    @Override
    public PageBean<WcmCommonAdminuser> pageDev(int nowPage, int pageSize) {
        ConditionEntity conditionEntity = new ConditionEntity();
        List<WhereEntity> whereEntities = new ArrayList<WhereEntity>();
        WhereEntity whereEntity = new WhereEntity();
        whereEntity.setField("menuNo");
        whereEntity.setOp(ConditionOp.eq);
        whereEntity.setValue("1");
        whereEntities.add(whereEntity);
        conditionEntity.setWhereEntities(whereEntities);
        return iWcmCommonAdminuserDao.page(nowPage, pageSize, conditionEntity);
    }

    @Override
    public WcmCommonAdminuser getAdminuserByUsern(String usern) {
        WcmCommonAdminuser adminuser = null;
        try {
            adminuser = iWcmCommonAdminuserDao.getAdminuserByUsern(usern);
        } catch (EmptyResultDataAccessException e) {
            adminuser = null;
        }
        return adminuser;
    }
}
